var TableOption = {
	createNew: function(action,pageCount){
		var tableOption={};		
		tableOption.option = {
		   'ajax'        : action,
		   'paging'      : true,
		   'lengthChange': true,
		   'searching'   : true,
		   'info'        : true,
		   'sort'        : true,
		   "language"	 : {
		   		"lengthMenu"		: "每页显示 _MENU_ 条记录",
		   		"zeroRecords"		: "无记录",
		   		"info"				: "共_PAGES_页,当前页_PAGE_",
		   		"infoEmpty"			: "无记录可用",
		        "infoFiltered"		: "(从_MAX_记录中筛选的结果)",
		        "sSearch"			: "查询",
		        "sProcessing"		: "查询中",
		        "sLoadingRecords"	: "加载中...",
		        "oPaginate": {				
					"sFirst"	: "第一页",
					"sLast"		: "最后一页",
					"sNext"		: "下一页",
					"sPrevious"	: "前一页"
				},
	        },
	        "iDisplayLength": pageCount,
	        "lengthMenu": [
	        	[pageCount, pageCount*2, pageCount*4, -1], 
	        	[pageCount, pageCount*2, pageCount*4, "All"]
	        ], 
		};
		return tableOption;
	}
};

var TableOpt={
	createNew:function(table,tableId){
		var tableOpt={};
		var rowSelector = tableId + " tbody";
		
		tableOpt.rowSelect=function(){
			$(rowSelector).on( 'click', 'tr', function () {
		       	if ( $(this).hasClass('selected') ) {
		           	$(this).removeClass('selected');
		       	}
		       	else {
		           	table.$('tr.selected').removeClass('selected');
		           	$(this).addClass('selected');
		       	}
		   	});
		};	
		
		tableOpt.add=function(btnId,modalId){
			$(btnId).click( function () {
			 	$(modalId).modal()
			});			
		};
		tableOpt.save=function(btnId,formId,action){
			$(btnId).click( function () {
			    var data = $(formId).serialize();			    
			    $.post(
		    		action, 
		    		data,
	                function (obj) {
	                    if (obj.status == 'true') {
	                        table.ajax.reload()
	                    } else {
	                        alert(obj.msg);
	                    }
	                }, 
	                'json'
		        );
		  	});
		};
		tableOpt.edit=function(btnId,action,idName,formId,modalId){
			$(btnId).click( function () {
				var selectedId;
				try{
					selectedId = table.row('.selected').data()[0];					
				}catch(err){
				 	return;
				}	
				
				data = {}
				data[idName] = selectedId
				
				$.getJSON(
					action,
					data,
					function(res){	
						var selector = formId + " input[type!='button']";
						$(selector).each(function(i,n){
							var id = $(this).attr('id');
							if (typeof id !== typeof undefined && id !== false) {
								if(id in res){
									$("#"+id).val(res[id])
								}
							}
						});				
						$('#modal-edit').modal()
					}		
				)
			});			
		};
		tableOpt.del=function(btnId,action,idName){
			$(btnId).click( function () {
				var selectedId;
				try{
					selectedId = table.row('.selected').data()[0];					
				}catch(err){
				 	return;
				}	
			    if(!confirm('确认删除吗?')){
			    	return;
			    }
			    data = {}
				data[idName] = selectedId
			    $.post(
			    	action, 
			    	data,
		            function (res) {
			            if (res.status == 'true') {
			                table.ajax.reload()
			            } else {
			                alert(obj.msg);
			            }
		            }, 
		            'json'
		        );
			    
		  	});			
		};
		
		tableOpt.update=function(btnId,action,formId){
			$(btnId).click( function () {
			    var data = $(formId).serialize();
			    $.post(
		    		action, 
		    		data,
	                function (res) {
	                    if (res.status == 'true') {
	                        table.ajax.reload()
	                    } else {
	                        alert(obj.msg);
	                    }
	                }, 
	                'json'
		        );
		  	});	 
		};	
		
		return tableOpt;
	}
}
